<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\AuthRole;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{

    public function index(Request $request)
    {
        $keyword = $request->get('keys');
        $limit = $request->get('limit', 10);
        $model = Admin::query();
        if ($keyword) {
            $model = $model->where('username', 'like', '%' . $keyword . '%');
        }
        $model = $model->orderBy('id', 'desc');
        $result = $model->paginate($limit);
        foreach ($result as $value) {
            if ($value['class_ids']) {
                $array = array_map('intval', explode(',', $value['class_ids']));
                $value['class_ids'] = $array;
            }
            if ($value['role_id']) {
                $value['role_name'] = AuthRole::where('role_id', $value['role_id'])->value('role_name');
            }
        }
        return showMsg('获取成功！', 200, $result);
    }

    public function detail(Request $request)
    {
        $id = $request->get('id');
        $result = Admin::findOrFail($id);
        if (!$result) {
            return showMsg('信息已删除', 201, $result);
        }
        return showMsg('操作成功', 200, $result);
    }

    public function beforeSave(Request $request)
    {
        $id = $request->get('id');
        $data = $request->all();
        $json['role_id'] = $data['role_id'];
        $json['username'] = $data['username'];
        $json['mobile'] = $data['mobile'];
        $json['truename'] = $data['truename'];
        if ($id) {
            $article = Admin::findOrFail($id);
            if ($data['password'] != $article['password']) {
                $json['password'] = bcrypt($data['password']);
            }
            if ($article->update($json)) {
                return showMsg('更新成功', 200);
            }
        } else {
            $json['password'] = bcrypt($data['password']);
            Admin::create($json);
            return showMsg('添加成功', 200);
        }
    }

    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return showMsg('请选择操作项', 201);
        }
        if (is_array($ids)) {
            foreach ($ids as $v) {
                Admin::destroy('id', $v);
            }
        } else {
            Admin::destroy('id', $ids);
        }
        return showMsg('删除成功', 200);
    }

    public function setStatus(Request $request)
    {
        $status = $request->get('status');
        $ids = $request->get('id');
        if ($status) {
            Admin::where('id', $ids)->update(['status' => 1]);
        } else {
            Admin::where('id', $ids)->update(['status' => 0]);
        }
        return showMsg('操作成功', 200);
    }

    public function setColumn(Request $request)
    {
        $role_id = $request->get('admin_id');
        $ids = $request->get('ids');
        $role_ids = implode(',', $ids);
        Admin::where('id', $role_id)->update(['class_ids' => $role_ids]);
        return showMsg('设置成功', 200);
    }

}
